package com.joshjung.hover.interactive
{
    import com.joshjung.collections.ILLIdentifiable;
    import com.joshjung.collections.LinkedList;
    import com.joshjung.hover.functional.Viewport;
    import com.joshjung.hover.interfaces.ILoopable;
    import com.joshjung.hover.interfaces.ITypeable;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Point;
    import flash.utils.getQualifiedClassName;

    /**
     *
     * @author jjung
     */
    public class PhysicObject extends Sprite implements ILoopable, ILLIdentifiable, ITypeable
    {
        public static var LAST_ID : int = 0;

        //---------------------------------------------------------------------
        //
        //  Variables
        //
        //---------------------------------------------------------------------
        public var speed : Point;
        public var friction : Number = 0.05;

        public var screenWrap : Boolean = true;
        public var destroyOffscreen : Boolean = false;

        public var viewport : Viewport;
        public var loopList : LinkedList;

        //---------------------------------------------------------------------
        //
        //  Properties
        //
        //---------------------------------------------------------------------
        private var _id : int = -1;

        public function get id() : int
        {
            return _id;
        }

        public function get type() : int
        {
            return -1;
        }

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function PhysicObject(viewport : Viewport, loopList : LinkedList)
        {
            super();

            _id = LAST_ID++;

            this.viewport = viewport;
            this.loopList = loopList;

            speed = new Point(0, 0);
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        public function loop(elapsed : Number) : void
        {
            // Friction
            speed.x -= (speed.x * friction) * elapsed;
            speed.y -= (speed.y * friction) * elapsed;

            // Position
            x += speed.x * elapsed;
            y += speed.y * elapsed;

            if (x > viewport.right)
            {
                if (screenWrap)
                    x = viewport.left;

                if (destroyOffscreen)
                    loopList.remove(this);
            }
            if (y > viewport.bottom)
            {
                if (screenWrap)
                    y = viewport.top;

                if (destroyOffscreen)
                    loopList.remove(this);
            }
            if (x < viewport.left)
            {
                if (screenWrap)
                    x = viewport.right;

                if (destroyOffscreen)
                    loopList.remove(this);
            }
            if (y < viewport.top)
            {
                if (screenWrap)
                    y = viewport.bottom;

                if (destroyOffscreen)
                    loopList.remove(this);
            }
        }

        public function push(direction : Point, force : Number)
        {
            speed.x += direction.x * force;
            speed.y += direction.y * force;
        }
    }
}
